package com.beauty.dressing.module.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseActivity;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.common.bean.UserInfoBean;
import com.beauty.dressing.common.utils.ImageBase64;
import com.beauty.dressing.common.view.AppDialog;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.mine.persenter.UserInfoControl;
import com.beauty.dressing.module.mine.persenter.UserInfoPresenter;
import com.beauty.dressing.rxandroid.api.ApiManager;
import com.beauty.dressing.rxandroid.rxbus.RxBus;
import com.beauty.dressing.rxandroid.rxbus.RxConstants;
import com.xiaolanba.commonlib.fresco.FrescoUtil;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.List;

public class UserInfoActivity extends BaseActivity {
    private User user;
    private TextView tv_user_phone;
    private TextView tv_user_nike_name;
    private Button btn_bind_sina;
    private Button btn_bind_qq;
    private SimpleDraweeView iv_avatar;
    private Button btn_bind_weChat;
    private UserInfoBean mBean;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_mine_info);
    }

    @Override
    public void findView() {
        TextView title_txt = (TextView) findViewById(R.id.title_txt);
        TextView right_txt = (TextView) findViewById(R.id.right_txt);
        tv_user_phone = (TextView) findViewById(R.id.tv_user_phone);
        tv_user_nike_name = (TextView) findViewById(R.id.tv_user_nike_name);
        btn_bind_sina = (Button) findViewById(R.id.btn_bind_sina);
        btn_bind_qq = (Button) findViewById(R.id.btn_bind_qq);
        iv_avatar = (SimpleDraweeView) findViewById(R.id.iv_avatar);
        btn_bind_weChat = findViewById(R.id.btn_bind_weChat);
        findViewById(R.id.iv_avatar).setOnClickListener(this);
        findViewById(R.id.left_img_btn).setOnClickListener(this);
        findViewById(R.id.nikename).setOnClickListener(this);
        btn_bind_weChat.setOnClickListener(this);
        btn_bind_qq.setOnClickListener(this);
        btn_bind_sina.setOnClickListener(this);
        title_txt.setText("个人信息");
        right_txt.setText("保存");
        right_txt.setTextColor(ContextCompat.getColor(context, R.color.white));
        right_txt.setVisibility(View.VISIBLE);
        right_txt.setOnClickListener(this);

        // 自定义图片加载器
        ISNav.getInstance().init(new ImageLoader() {
            @Override
            public void displayImage(Context context, String path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });

    }

    @Override
    public void initData() {
        //获取当前登录用户信息
        user = LBController.getInstance().getCacheManager().getLoginUser();
        TextView tv_userID = (TextView) findViewById(R.id.tv_userID);
        tv_user_phone.setOnClickListener(this);
        presenter.memberInfo(user.token, user.member_id);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nikename:
                changeNikename();
                break;
            case R.id.left_img_btn:
                this.finish();
                break;
            case R.id.right_txt:
                //保存
                break;
            case R.id.tv_user_phone:
                //修改电话
                startActivity(new Intent(context, ChangePhoneActivity.class));
                break;
            case R.id.btn_bind_sina:
                context.showToast("绑定");
                break;
            case R.id.btn_bind_qq:
                context.showToast("绑定");
                break;
            case R.id.btn_bind_weChat:

                break;
            case R.id.iv_avatar:
                ISListConfig config = initImageSelect();
                // 跳转到图片选择器
                ISNav.getInstance().toListActivity(context, config, 666);
                break;
        }
    }

    /**
     * 修改昵称
     */
    private void changeNikename() {
        final AppDialog appDialog = new AppDialog(context);
        appDialog.createConfirmDialog(context, "修改昵称", user.userNickName == null ? "" : user.userNickName,
                context.getString(R.string.cancel), context.getString(R.string.ok), new AppDialog.OnConfirmButtonDialogListener() {
                    @Override
                    public void onLeftButtonClick() {
                        appDialog.dismiss();
                    }

                    @Override
                    public void onRightButtonClick(String name) {
//                                presenter.editName(String.valueOf(user.member_id), name, user.token);
                        //保存用户信息
                        presenter.editName(String.valueOf(user.member_id), name, user.token);
                    }
                });
        appDialog.show();
    }

    private ISListConfig initImageSelect() {
        // 自由配置选项
        return new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.GRAY)
                // “确定”按钮文字颜色
                .btnTextColor(Color.BLUE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.parseColor("#3F51B5"))
                // 返回图标ResId
                .backResId(R.drawable.return_back_white)
                // 标题
                .title("图片")
                // 标题文字颜色
                .titleColor(Color.WHITE)
                // TitleBar背景色
                .titleBgColor(Color.parseColor("#3F51B5"))
                // 裁剪大小。needCrop为true的时候配置
                .cropSize(1, 1, 200, 200)
                .needCrop(true)
                // 第一个是否显示相机，默认true
                .needCamera(false)
                // 最大选择图片数量，默认9
                .maxNum(9)
                .build();


    }

    private UserInfoPresenter presenter = new UserInfoPresenter(new UserInfoControl() {
        @Override
        public void onEditName(boolean isSuccess, int code, String message, List<Object> list) {
            super.onEditName(isSuccess, code, message, list);
            dismissProgressDialog();
            if (message == null || message.length() == 0) {
                return;
            }
            Log.e(TAG, "code: " + code + " message:" + message);
            if (code == 200) {
                //更新user数据
                user.userNickName = message;
                //更新User表的数据
                LBController.getInstance().getDaoManager().getDaoSession().getLoginUserDao().updateLoginUser(user);
                //发送一个请求
                RxBus.getDefault().postWithCode(RxConstants.ACTION_CHANGENICKNAME_SUCCESS);
                //更新页面数据
                tv_user_nike_name.setText(message);
            } else if (code == 100) {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
        }


        @Override
        public void onEditImage(boolean isSuccess, int code, String message) {
            dismissProgressDialog();
            if (message == null || message.length() == 0) {
                return;
            }
            //更新表的数据
            user.image = message;
            //更新User表的数据
            LBController.getInstance().getDaoManager().getDaoSession().getLoginUserDao().updateLoginUser(user);
            //发送一个请求
//            RxBus.getDefault().postWithCode(RxConstants.ACTION_CHANGENICKNAME_SUCCESS);
            FrescoUtil.loadImage(iv_avatar, ApiManager.HEAR_URL + message);
        }

        @Override
        public void onMemberInfo(boolean isSuccess, int code, UserInfoBean bean) {
            super.onMemberInfo(isSuccess, code, bean);
            mBean = bean;
            TextView tv_userID = findViewById(R.id.tv_userID);
            tv_userID.setText(String.valueOf(mBean.getMember_id()));
            FrescoUtil.loadImage(iv_avatar, ApiManager.HEAR_URL + mBean.getImage());
            tv_user_phone.setText(mBean.getAccount());
            tv_user_nike_name.setText(mBean.getMembername());
            if (mBean.getBind_weixin() != null) {
                btn_bind_weChat.setBackground(null);
                btn_bind_weChat.setText("已绑定");
            }
            if (mBean.getBind_qq() != null) {
                btn_bind_qq.setBackground(null);
                btn_bind_qq.setText("已绑定");
            }
            if (mBean.getBind_alipay() != null) {
                btn_bind_sina.setBackground(null);
                btn_bind_sina.setText("已绑定");
            }

        }
    });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode == 666 && resultCode == RESULT_OK && data != null) {
            String path = data.getStringArrayListExtra("result").get(0);
            presenter.editImage(user.account, ImageBase64.getImageToBase64(path),
                    path.substring(path.length() - 3, path.length()), user.token);
        }
    }
}
